const app = getApp();
//引入插件：微信同声传译
const plugin = requirePlugin('WechatSI');

//获取全局唯一的语音识别管理器recordRecoManager
const manager = plugin.getRecordRecognitionManager();

var util= require('../../utils/util.js')

const apikey = "X1O48XA6TwNx76dYFizHvM3ReHM="//我的
const devicesid  = '1038455179'//我的
let ledpwm = 0
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    content:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    this.initRecord();
  },
  // 手动输入内容
  
  //识别语音 -- 初始化
  initRecord: function () {
    const that = this;
    // 有新的识别内容返回，则会调用此事件
    manager.onRecognize = function (res) {
      console.log(res)
    }
    // 正常开始录音识别时会调用此事件
    manager.onStart = function (res) {
      console.log("成功开始录音识别", res)
    }
    // 识别错误事件
    manager.onError = function (res) {
      console.error("error msg", res)
    }
    //识别结束事件
    manager.onStop = function (res) {
      if(res.result.toString().indexOf("开")>=0&&res.result.toString().indexOf("灯")>=0){
        let data={
          "datastreams": [  
          {"id": "ledbtn","datapoints":[{"value": 1}]},
          ]
         }
        console.log('灯已打开');
        
        wx.request({
          url:'https://api.heclouds.com/devices/1038455179/datapoints',
          header: {
            'content-type': 'application/json',
            'api-key':apikey
          },
          method: 'POST',
          data: JSON.stringify(data),//data里面是要上传云平台的数据
          success(res){
            console.log("成功",res.data)
            util.playTTS("灯已打开");
          },
          fail(res){
            console.log("失败",res)
          }
        })
      }
      if(res.result.toString().indexOf("关")>=0&&res.result.toString().indexOf("灯")>=0){
        let data={
          "datastreams": [  
          {"id": "ledbtn","datapoints":[{"value": 0}]},
          ]
         }
        
        wx.request({
          url:'https://api.heclouds.com/devices/1038455179/datapoints',
          header: {
            'content-type': 'application/json',
            'api-key':apikey
          },
          method: 'POST',
          data: JSON.stringify(data),//data里面是要上传云平台的数据
          success(res){
            console.log("成功",res.data)
            util.playTTS("灯已关闭");
          },
          fail(res){
            console.log("失败",res)
          }
        })
      }
      
      if(res.result.toString().indexOf("太亮了")>=0){
       
          let data={
            "datastreams": [  
            {"id": "ledpwm","datapoints":[{"value": 20}]},
            ]
           }
          
          wx.request({
            url:'https://api.heclouds.com/devices/1038455179/datapoints',
            header: {
              'content-type': 'application/json',
              'api-key':apikey
            },
            method: 'POST',
            data: JSON.stringify(data),//data里面是要上传云平台的数据
            success(res){
              util.playTTS("已为您调暗灯光");
            },
            fail(res){
              console.log("失败",res)
            }
          })
      }

      if(res.result.toString().indexOf("太暗了")>=0){
       
          let data={
            "datastreams": [  
            {"id": "ledpwm","datapoints":[{"value": 400}]},
            ]
           }
          wx.request({
            url:'https://api.heclouds.com/devices/1038455179/datapoints',
            header: {
              'content-type': 'application/json',
              'api-key':apikey
            },
            method: 'POST',
            data: JSON.stringify(data),//data里面是要上传云平台的数据
            success(res){
              util.playTTS("已为您调亮灯光");
            },
            fail(res){
              console.log("失败",res)
            }
          })
      }
      if(res.result.toString().indexOf("开")>=0&&res.result.toString().indexOf("风扇")>=0){
        let data={
          "datastreams": [  
          {"id": "fan","datapoints":[{"value": 1}]},
          ]
         }
        console.log('灯已打开');
        
        wx.request({
          url:'https://api.heclouds.com/devices/1038455179/datapoints',
          header: {
            'content-type': 'application/json',
            'api-key':apikey
          },
          method: 'POST',
          data: JSON.stringify(data),//data里面是要上传云平台的数据
          success(res){
            console.log("成功",res.data)
            util.playTTS("风扇已打开");
          },
          fail(res){
            console.log("失败",res)
          }
        })
      }
      if(res.result.toString().indexOf("关")>=0&&res.result.toString().indexOf("风扇")>=0){
        let data={
          "datastreams": [  
          {"id": "fan","datapoints":[{"value": 0}]},
          ]
         }
        
        wx.request({
          url:'https://api.heclouds.com/devices/1038455179/datapoints',
          header: {
            'content-type': 'application/json',
            'api-key':apikey
          },
          method: 'POST',
          data: JSON.stringify(data),//data里面是要上传云平台的数据
          success(res){
            console.log("成功",res.data)
            util.playTTS("风扇已关闭");
          },
          fail(res){
            console.log("失败",res)
          }
        })
      }
      if (res.result == '') {
        wx.showModal({
          title: '提示',
          content: '听不清楚，请重新说一遍！',
          showCancel: false,
          success: function (res) {}
        })
        util.playTTS("听不清楚，请您再说一遍");
        return;
      }
      // var text = that.data.content + res.result;
      that.setData({
        content: res.result
      })
    }
  },
  //开始录音
  touchStart: function (e) {
    this.setData({
      recordState: true  //录音状态
    })
    // 语音开始识别
    manager.start({
      lang: 'zh_CN',// 识别的语言，目前支持zh_CN en_US zh_HK sichuanhua
    })
  },
  //结束录音
  touchEnd: function (e) {
       // 语音结束识别
    manager.stop();
  },

})
